package com.google.translate;


import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.discuz.spider.WebClientAgent;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;

public class T {

	public static void main(String[] args) throws IOException, InterruptedException {
		List<String> rs = FileUtils.readLines(new File("/User/only/share/siwameitui_postlist.txt"));
		
		for(String e :rs){
			String [] as = e.split("|");
			if(as.length==2){
				System.out.println(as[1]);
			}else{
				System.out.println(e);
			}
		}

	}
	
	static WebClient client = WebClientAgent.get();

	
	public static String tramslate(File file) throws IOException, InterruptedException{
		String dest = FileUtils.readFileToString(file);
		String result =null;
		
		HtmlPage page = client.getPage("http://translate.google.cn/#zh-CN/en/");
		//设置源语言zh_cn
		HtmlSelect yu = (HtmlSelect)page.getFirstByXPath("id('gt-sl')");
		
		yu.setSelectedAttribute(yu.getOptionByValue("zh-CN"), true);
		
		HtmlForm form = page.getFormByName("text_form");
		HtmlTextArea textArea = form.getTextAreaByName("text");
		textArea.blur();
		textArea.setText(dest);
		
		
		//设置目标语言
		HtmlSelect t = (HtmlSelect)page.getFirstByXPath("id('gt-tl')");
		HtmlOption opt = t.getOptionByValue("en");
		opt.setAttribute("selected"," ture");
		t.removeOption(t.getOptionSize()-1);
		HtmlSubmitInput sumbit = (HtmlSubmitInput)page.getFirstByXPath("id('gt-submit')");
		
		
		HtmlPage p = sumbit.click();
		
		Thread.sleep(3000);
		HtmlElement resultEL = p.getFirstByXPath("id('result_box')");
		
		result = resultEL.asText();
		
		return result;
	}
	public static String tramslate(String dest) throws IOException, InterruptedException{
		String result =null;
		
		HtmlPage page = client.getPage("http://translate.google.cn/#zh-CN/en/");
		//设置源语言zh_cn
		HtmlSelect yu = (HtmlSelect)page.getFirstByXPath("id('gt-sl')");
		
		yu.setSelectedAttribute(yu.getOptionByValue("zh-CN"), true);
		
		HtmlForm form = page.getFormByName("text_form");
		HtmlTextArea textArea = form.getTextAreaByName("text");
		textArea.blur();
		textArea.setText(dest);
		
		
		//设置目标语言
		HtmlSelect t = (HtmlSelect)page.getFirstByXPath("id('gt-tl')");
		HtmlOption opt = t.getOptionByValue("en");
		opt.setAttribute("selected"," ture");
		t.removeOption(t.getOptionSize()-1);
		HtmlSubmitInput sumbit = (HtmlSubmitInput)page.getFirstByXPath("id('gt-submit')");
		
		
		HtmlPage p = sumbit.click();
		
		Thread.sleep(3000);
		HtmlElement resultEL = p.getFirstByXPath("id('result_box')");
		
		result = resultEL.asText();
		
		return result;
	}
}

